<template>
  <div class="container">
    <div class="containerselect">
      <div class="containerselects">
        <el-select clearable v-model="schoolId" placeholder="请选择学校" @change="getEquipmentList">
          <el-option v-for="item in optionsst" :key="item.shId" :label="item.shName" :value="item.shId">
          </el-option>
        </el-select>
        <el-select clearable @change="getEquipmentList" v-model="equipmentType" placeholder="设备类型"
          style="margin-left: 1rem">
          <el-option v-for="item in optionsere" :key="item.papplication" :label="item.pname" :value="item.papplication">
          </el-option>
        </el-select>
      </div>
      <div class="containerbotton">
        <el-button size="small" type="primary" @click="addActivationCode" style="margin-bottom: 20px; float: right">添加设备
        </el-button>
      </div>
    </div>

    <el-table v-loading="isLoading" :data="tableData" style="width: 100%" :row-class-name="xxxTableRowClassName"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }">
      <el-table-column prop="equipmentName" label="设备名称" width="width" align="center">
      </el-table-column>
      <el-table-column prop="equipmentCode" label="设备标识码" width="width" align="center">
      </el-table-column>
      <el-table-column prop="equipmentType" label="设备类型" width="width" align="center">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.equipmentType === "1"
              ? "大屏"
              : scope.row.equipmentType === "2"
                ? "摄像头"
                : scope.row.equipmentType === "3"
                  ? "运算设备"
                  : scope.row.equipmentType === "4"
                    ? "其他"
                    : "暂无"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="equipmentIpAddress" label="设备IP地址" width="width" align="center">
      </el-table-column>
      <el-table-column prop="equipmentRemark" label="设备备注" width="width" align="center">
      </el-table-column>

      <el-table-column prop="equipmentYardAddress" label="二维码" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.equipmentYardAddress" style="width: 50%; height: 50%" v-image-preview alt="" class="img" />
        </template>
      </el-table-column>
      <el-table-column prop="pullstreamsAddress" label="视频流地址" width="width" align="center">
        <template slot-scope="scope">
          <span @click="handler(scope.row.pullstreamsAddress)">
            {{ scope.row.pullstreamsAddress }}
          </span>
        </template>
      </el-table-column>

      <el-table-column prop="equipmentSchoolName" label="所属学校" width="width" align="center">
      </el-table-column>
      <el-table-column prop="equipmentScenarioId" label="所属场所" width="width" align="center">
      </el-table-column>
      <el-table-column prop="" label="支持项" width="width" align="center">
      </el-table-column>

      <el-table-column label="操作" width="145px" align="center">
        <template slot-scope="{ row }">
          <el-button plain v-if="row.equipmentType == 2" type="primary" size="small"
            @click="compile($event, row)">修改</el-button>
          <el-button type="danger" size="small" plain v-if="row.equipmentType == 2"
            @click="delActivationCode($event, row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="form.equipmentId ? '修改设备' : '添加设备'" :visible.sync="dialogFormVisible" width="50%"
      :close-on-click-modal="false">
      <el-form label-position="left" :model="form" :rules="rules" ref="ruleForm" label-width="140px">
        <el-form-item label="设备名称:" prop="equipmentName">
          <el-input style="width: 80%" v-model="form.equipmentName" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="设备IP地址:" prop="equipmentIpAddress">
          <el-input style="width: 80%" v-model="form.equipmentIpAddress" placeholder="请输入设备IP地址"></el-input>
        </el-form-item>
        <el-form-item label="设备类型:" prop="equipmentType">
          <el-select v-model="form.equipmentType" placeholder="请选择设备类型" style="width: 80%">
            <el-option v-for="item in optionsere" :key="item.papplication" :label="item.pname" :value="item.papplication">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属场所:" prop="equipmentSchoolId" label-width="151px">
          <el-select style="width: 39%" @change="change" v-model="form.equipmentSchoolId" placeholder="学校">
            <el-option v-for="item in optionsst" :key="item.shId" :label="item.shName" :value="item.shId">
            </el-option>
          </el-select>
          <el-select style="width: 39%; margin-left: 2%" v-model="form.equipmentScenarioId" placeholder="场所">
            <el-option v-for="item in optionssr" :key="item.scenarioId" :label="item.scenarioName"
              :value="item.scenarioId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="主机标识:" prop="equipmentParentsCode" label-width="151px">
          <el-input v-model="form.equipmentParentsCode" placeholder="单行输入" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="摄像头标识:" prop="cameraCode">
          <el-input v-model="form.cameraCode" placeholder="单行输入" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="备注:" prop="equipmentRemark">
          <el-input v-model="form.equipmentRemark" placeholder="单行输入" style="width: 80%">
          </el-input>
        </el-form-item>
        <el-form-item label="用户名:" prop="cameraAcc">
          <el-input v-model="form.cameraAcc" placeholder="单行输入" style="width: 80%">
          </el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="cameraPssd">
          <el-input v-model="form.cameraPssd" placeholder="单行输入" style="width: 80%">
          </el-input>
        </el-form-item>
        <el-form-item label="rstp用户名:" prop="cameraRstpAcc">
          <el-input v-model="form.cameraRstpAcc" placeholder="单行输入" style="width: 80%">
          </el-input>
        </el-form-item>
        <el-form-item label="rstp密码:" prop="cameraRstpPssd">
          <el-input v-model="form.cameraRstpPssd" placeholder="单行输入" style="width: 80%">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button :loading="disable" size="small" type="primary" @click="save()">{{ disable ? "加载中" : "添 加" }}
        </el-button>
      </div>
    </el-dialog>

    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
import {
  postselectSchool,
  postSchoolKong,
  selectSchool,
  getEquipmentList,
  addEquipment,
  updateEquipment,
  deleteEquipment,
  getSchoolKongjian,
  postselectdree,
} from "@/api/device";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      equipmentType: '',

      value: "",
      disable: false,
      total: -1,
      loading: false,
      page: {
        pageSize: 10,
        pageIndex: 1,
      },

      dialogFormVisible: false,
      schoolList: [],
      tableData: [],

      form: {
        equipmentSchoolId: "",
        // equipmentId: "",
        equipmentName: "",
        // equipmentCode: "",
        // equipmentYardAddress: "",
        equipmentRemark: "",
        equipmentType: "",
        equipmentIpAddress: "",
        equipmentScenarioId: "",
        // equipmentSchoolName: "",
        // updateId: "",
        // updateTime: "",
        // pullstreamsAddress: "",

        cameraAcc: "",
        cameraPssd: "",
        cameraRstpAcc: "",
        cameraRstpPssd: "",
      },
      rules: {
        equipmentName: [
          {
            required: true,
            message: "请输入名称",
          },
        ],
        equipmentSchoolId: [
          {
            required: true,
            message: "请选择学校",
          },
        ],
        equipmentIpAddress: [
          {
            required: true,
            message: "请输入设备ID地址",
          },
        ],
        value: [
          {
            required: true,
            message: "请选择所属学校场所",
          },
        ],
        equipmentType: [
          {
            required: true,
            message: "请选择设备类型",
          },
        ],
        cameraAcc: [
          {
            required: true,
            message: "请输入用户名",
          },
        ],
        cameraPssd: [
          {
            required: true,
            message: "请输入密码",
          },
        ],
        cameraRstpAcc: [
          {
            required: true,
            message: "请输入rstp用户名",
          },
        ],
        cameraRstpPssd: [
          {
            required: true,
            message: "请输入rstp密码",
          },
        ],

        equipmentParentsCode: [
          {
            required: true,
            message: "请输入主机标识",
          },
        ],
        cameraCode: [
          {
            required: true,
            message: "请输入标识",
          },
        ],
        equipmentRemark: [
          {
            required: true,
            message: "请输入备注",
          },
        ],
      },
      optionsst: [],
      optionssr: [],
      optionsere: [],
      isLoading: false,
      optionsProps: {
        label: "equipmentSchoolName",
        children: "equipmentScenarioId",
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getEquipmentList();
    this.getSchoolChangsuo();

    this.getSelectSchool();
  },
  mounted() { },
  methods: {
    getSchoolChangsuo() {
      postSchoolKong({}).then((res) => {
        console.log(res, "");
        if (res.code == 200) {
          this.optionsere = res.result;
        }
      });
      postselectSchool({}).then((res) => {
        console.log(res, "");
        if (res.code == 200) {
          this.optionsst = res.result;
        }
      });
    },
    change(v) {
      this.form.equipmentScenarioId = "";
      let data = {
        schoolId: v,
      };
      postselectdree(data).then((res) => {
        console.log(res, "");
        if (res.code == 200) {
          this.optionssr = res.result;
        }
      });
    },

    handler(a) {
      console.log(a);
      this.$router.push({ path: "vdeostream", query: { rtsp: a } });
    },
    compile(evt, row) {
      console.log(row);
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.dialogFormVisible = true;
      this.form = { ...row };
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    getEquipmentList() {
      let data = {
        ...this.page,
        schoolId: this.schoolId,
        equipmentType: this.equipmentType,

      };
      console.log(data, 'eeeee')
      this.isLoading = true;
      getEquipmentList(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result;
          this.total = res.total;
          console.log(this.total, "这是一个数据");
          this.isLoading = false;
        }
      });
    },
    getSelectSchool() {
      let data = {
        userId: this.$store.state.user.USER_INFO.id,
      };
      selectSchool(data).then((res) => {
        this.schoolList = res.result;
      });
    },
    addActivationCode() {
      this.dialogFormVisible = true;
      (this.form = {
        equipmentSchoolId: "",
        equipmentName: "",
        equipmentRemark: "",
        equipmentType: "",
        equipmentIpAddress: "",
        equipmentScenarioId: "",
        cameraAcc: "",
        cameraPssd: "",
        cameraRstpAcc: "",
        cameraRstpPssd: "",
      }),
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate();
        });
    },
    delActivationCode(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          let data = {
            equipmentId: row.equipmentId,
          };
          deleteEquipment(data).then((res) => {
            if (res.code === 200) {
              this.getEquipmentList();
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    save() {
      let data = {
        ...this.form,
      };
      console.log(this.form);
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;
          if (this.form.equipmentId) {
            addEquipment(data).then((res) => {
              if (res.code === 200) {
                this.dialogFormVisible = false;
                (this.form = {
                  equipmentSchoolId: "",
                  equipmentName: "",
                  equipmentRemark: "",
                  equipmentType: "",
                  equipmentIpAddress: "",
                  equipmentScenarioId: "",
                  cameraAcc: "",
                  cameraPssd: "",
                  cameraRstpAcc: "",
                  cameraRstpPssd: "",
                }),
                  this.getEquipmentList();
                this.$message.success("保存成功");
                this.disable = false;
              } else {
                this.disable = false;
                this.dialogFormVisible = false;
                this.$message.error(res.message);
              }
            });
          } else {
            addEquipment(data).then((res) => {
              if (res.code === 200) {
                this.dialogFormVisible = false;
                this.getEquipmentList();
                this.$message.success("保存成功");
                this.disable = false;
              } else {
                this.disable = false;
                this.dialogFormVisible = false;
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getEquipmentList();
    },
    changePage(page) {
      this.page.pageIndex = page;
      this.getEquipmentList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}

::v-deep .el-upload-list__item {
  display: none;
}

::v-deep .el-form-item__label {
  padding-left: 40px;
}

.container {
  .containerselect {
    display: flex;
    justify-content: space-between;

    .containerselects {}

    .containerbotton {
      margin-top: 0.5rem;
    }
  }
}
</style>
